package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import cn.hutool.json.JSONUtil;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.*;
import com.zzyl.nursing.dto.*;
import com.zzyl.nursing.mapper.BedMapper;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.util.CodeGenerator;
import com.zzyl.nursing.vo.CheckInConfigVo;
import com.zzyl.nursing.vo.CheckInDetailVo;
import com.zzyl.nursing.vo.CheckInElderVo;
import com.zzyl.nursing.vo.ElderFamilyVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.CheckInMapper;
import com.zzyl.nursing.service.ICheckInService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 入住Service业务层处理
 * 
 * @author alexis
 * @date 2025-06-07
 */
@Service
public class CheckInServiceImpl extends ServiceImpl<CheckInMapper, CheckIn> implements ICheckInService
{
    @Autowired
    private CheckInMapper checkInMapper;

    /**
     * 查询入住
     * 
     * @param id 入住主键
     * @return 入住
     */
    @Override
    public CheckIn selectCheckInById(Long id)
    {
        return checkInMapper.selectById(id);
    }

    /**
     * 查询入住列表
     * 
     * @param checkIn 入住
     * @return 入住
     */
    @Override
    public List<CheckIn> selectCheckInList(CheckIn checkIn)
    {
        return checkInMapper.selectCheckInList(checkIn);
    }

    /**
     * 新增入住
     * 
     * @param checkIn 入住
     * @return 结果
     */
    @Override
    public int insertCheckIn(CheckIn checkIn)
    {
        return checkInMapper.insert(checkIn);
    }

    /**
     * 修改入住
     * 
     * @param checkIn 入住
     * @return 结果
     */
    @Override
    public int updateCheckIn(CheckIn checkIn)
    {
        return checkInMapper.updateById(checkIn);
    }

    /**
     * 批量删除入住
     * 
     * @param ids 需要删除的入住主键
     * @return 结果
     */
    @Override
    public int deleteCheckInByIds(Long[] ids)
    {
        return checkInMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除入住信息
     * 
     * @param id 入住主键
     * @return 结果
     */
    @Override
    public int deleteCheckInById(Long id)
    {
        return checkInMapper.deleteById(id);
    }

    @Autowired
    private ElderServiceImpl elderService;
    @Autowired
    private BedServiceImpl bedService;
    @Autowired
    private CheckInServiceImpl checkInService;
    @Autowired
    private ContractServiceImpl contractService;
    @Autowired
    private CheckInConfigServiceImpl checkInConfigService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int apply(CheckInApplyDto checkInApplyDto) {
//        System.out.println("apply: " + checkInApplyDto);
        //入住配置
        CheckInConfigDto checkInConfigDto = checkInApplyDto.getCheckInConfigDto();
        //签约办理
        CheckInContractDto checkInContractDto = checkInApplyDto.getCheckInContractDto();
        //老人信息
        CheckInElderDto checkInElderDto = checkInApplyDto.getCheckInElderDto();
       //家属信息
        List<ElderFamilyDto> elderFamilyDtoList = checkInApplyDto.getElderFamilyDtoList();

        Elder elder = elderService.lambdaQuery()
                .eq(Elder::getIdCardNo, checkInElderDto.getIdCardNo())
                .one();
        if (elder != null&&elder.getStatus() == 1) {
            throw new RuntimeException("该老人已入住");
        }

        Bed bed = new Bed();
        bed.setBedStatus(1);
        bed.setId(checkInConfigDto.getBedId());
        bedService.updateById(bed);

        Bed dbBed=bedService.getById(bed.getId());
        if(dbBed==null){
            throw new RuntimeException("床位不存在");
        }

        //新增或更新老人
        Elder elder1 = new Elder();
        BeanUtils.copyProperties(checkInElderDto, elder1);
        elder1.setBedId(bed.getId());
        elder1.setBedNumber(dbBed.getBedNumber());
        elder1.setStatus(1);
        elderService.saveOrUpdate(elder1);

        //新增签约办理
        Contract contract = new Contract();
        BeanUtils.copyProperties(checkInContractDto, contract);
        contract.setElderId(elder1.getId());
        contract.setElderName(elder1.getName());
        contract.setContractNumber("HT"+CodeGenerator.generateContractNumber());
        LocalDateTime star=checkInConfigDto.getStartDate();
        LocalDateTime end=checkInConfigDto.getEndDate();
        contract.setStartDate(star);
        contract.setEndDate(end);
        contract.setStatus(LocalDateTime.now().isAfter(star)?1:0);
        contractService.save(contract);

        //新增入住信息
        CheckIn checkIn = new CheckIn();
        checkIn.setElderId(elder1.getId());
        checkIn.setElderName(elder1.getName());
        checkIn.setIdCardNo(elder1.getIdCardNo());
        checkIn.setStartDate(checkInConfigDto.getStartDate());
        checkIn.setEndDate(checkInConfigDto.getEndDate());
        checkIn.setNursingLevelName(checkInConfigDto.getNursingLevelName());
        checkIn.setBedNumber(dbBed.getBedNumber());
        checkIn.setStatus(0);
        checkIn.setRemark(JSONUtil.toJsonStr(elderFamilyDtoList));
        checkInService.save(checkIn);

        //新增入住配置

        CheckInConfig checkInConfig = new CheckInConfig();
        BeanUtils.copyProperties(checkInConfigDto, checkInConfig);
        checkInConfig.setCheckInId(checkIn.getId());
        checkInConfigService.save(checkInConfig);
        return 1;
    }

    @Override
    public CheckInDetailVo detail(Long id) {
        CheckInDetailVo checkInDetailVo = new CheckInDetailVo();
       CheckIn checkIn = checkInService.getById(id);

        CheckInElderVo checkInElderVo = new CheckInElderVo();
        Elder elde= elderService.getById(checkIn.getElderId());
        BeanUtils.copyProperties(elde, checkInElderVo);
        checkInDetailVo.setCheckInElderVo(checkInElderVo);

        checkIn.getRemark();
        List<ElderFamilyVo> elderFamilyDtoList = JSONUtil.toList(checkIn.getRemark(), ElderFamilyVo.class);
        checkInDetailVo.setElderFamilyVoList(elderFamilyDtoList);
        System.out.println("elderFamilyDtoList = " + elderFamilyDtoList);

        CheckInConfigVo checkInConfigVo = new CheckInConfigVo();
        BeanUtils.copyProperties(checkInConfigService.getById(checkIn.getId()), checkInConfigVo);
        checkInDetailVo.setCheckInConfigVo(checkInConfigVo);

        Contract contract = contractService.lambdaQuery()
                .eq(Contract::getElderId, checkIn.getElderId())
                .one();

        checkInDetailVo.setContract(contract);






        System.out.println("checkInElderVo = " + checkInElderVo);
    return checkInDetailVo;
    }
}
